// 审核计划表
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="内审日期">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inAudit:correct:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['inAudit:correct:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="printBox"
          v-hasPermi="['inAudit:correct:print']"
        >打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="流程状态" min-width="150">
        <template slot-scope="scope">
          <el-tag size="mini">{{ scope.row.statusName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="200" show-overflow-tooltip label="年度">
        <template slot-scope="scope">
          <span>{{ scope.row.planYear }}年度内部工作计划</span>
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="责任部门" prop="deptName" min-width="150" />
      <el-table-column show-overflow-tooltip label="责任部门负责人" min-width="150" prop="userName" />
      <!-- <el-table-column label="是否启用" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.effectStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['inAudit:correct:info']"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            v-if="scope.row.status === 'draft'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['checkContentSetting:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.status === 'draft'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['inAudit:correct:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 打印模板位置 -->
    <PrintBox :checkNum="9" :printRow="printList" :isPageSize="false" id="printBox">
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 10px">不符合报告及纠正措施表 STET/1/9-1</div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table
          style="border-collapse: collapse; table-layout: fixed"
          border="1"
          cellspacing="0"
          class="table-page"
        >
          <tr>
            <td style="width: 55mm">责任部门</td>
            <td style="width: 80mm">{{ scope.item.deptName }}</td>
            <td style="width: 80mm">责任部门负责人</td>
            <td style="width: 50mm">{{ scope.item.userName }}</td>
          </tr>
          <tr>
            <td colspan="4" style="height: 20mm">
              <div style="text-align: left">
                <div>不符合事实描述:</div>
                <div style="height: 50px">
                  {{
                  scope.item.innerCorrectItem
                  .map((e) => e.errorDesc)
                  .toString()
                  }}
                </div>
                <div style>
                  条款号:{{
                  scope.item.innerCorrectItem
                  .map((e) => e.orderContent)
                  .toString()
                  }}
                </div>
                <div style="display: flex">
                  不符合类型:
                  <div
                    v-for="item in dict.type.sys_error_type"
                    style="
                      margin-left: 10mm;
                      display: flex;
                      align-items: center;
                    "
                  >
                    <input
                      type="checkbox"
                      :checked="
                        checkItem(item.value, scope.item.innerCorrectItem)
                      "
                    />
                    {{ item.label }}
                  </div>
                </div>
              </div>

              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 20px;
                "
              >
                <div style="width: 55mm; position: relative">
                  检查人：
                  <img
                    style="width: 80px; position: absolute; top: -10px"
                    :src="$VUE_APP_IMG_API + scope.item.esign"
                  />
                </div>
                <div style="width: 50mm">日期：{{ checkHistoryNode(scope, "start").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="4" style="height: 17mm">
              <div style="text-align: left">
                <div>不合格原因分析:</div>
                <div style="height: 80px">
                  {{
                  scope.item.innerCorrectItem.map((e) => e.reason).toString()
                  }}
                </div>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 30px;
                  position: relative;
                "
              >
                <div style="width: 55mm">
                  责任部门/人：
                  <template v-if="CheckApprove(scope, 'zrbmfzr').ShowImg">
                    <img
                      style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'zrbmfzr').ApproveImg"
                    />
                  </template>
                </div>
                <div style="width: 50mm">日期：{{ checkHistoryNode(scope, "zrbmfzr").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="4" style="height: 20mm">
              <div style="text-align: left">
                <div>纠正措施和计划完成日期:</div>
                <div style="height: 100px">
                  <span
                    v-for="(item, index) in scope.item.innerCorrectItem"
                    v-if="item.correct && item.finishDate"
                  >
                    {{ item.correct }}({{ item.finishDate }})
                    <text v-if="index !== scope.item.innerCorrectItem.length - 1">,</text>
                  </span>
                </div>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 30px;
                "
              >
                <div style="width: 55mm; position: relative">
                  责任部门/人：
                  <template v-if="CheckApprove(scope, 'bmfzr').ShowImg">
                    <img
                      style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'bmfzr').ApproveImg"
                    />
                  </template>
                </div>
                <div style="width: 50mm">日期：{{ checkHistoryNode(scope, "bmfzr").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="4" style="height: 20mm">
              <div style="text-align: left">
                <div>批准意见:</div>
                <div style="height: 80px; position: relative">
                  <template
                    v-if="CheckApprove(scope, 'pz').ShowMessage"
                  >{{ CheckApprove(scope, "pz").MessageText }}</template>
                </div>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 18px;
                "
              >
                <div style="width: 55mm; position: relative">
                  批准人：
                  <template v-if="CheckApprove(scope, 'pz').ShowImg">
                    <img
                      :src="CheckApprove(scope, 'pz').ApproveImg"
                      style="width: 80px; position: absolute; top: -10px"
                    />
                  </template>
                </div>
                <div style="width: 50mm">日期：{{ checkHistoryNode(scope, "pz").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="4" style="height: 20mm">
              <div style="text-align: left">
                <div>纠正措施完成情况以附件:</div>
                <div style="height: 80px">
                  {{
                  scope.item.innerCorrectItem.map((e) => e.info).toString()
                  }}
                </div>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 18px;
                "
              >
                <div style="width: 55mm; position: relative">
                  部门负责人：
                  <template v-if="CheckApprove(scope, 'bmfzrhb').ShowImg">
                    <img
                      style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'bmfzrhb').ApproveImg"
                    />
                  </template>
                </div>
                <div style="width: 50mm">日期：{{ checkHistoryNode(scope, "bmfzrhb").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="4" style="height: 20mm">
              <div style="text-align: left">
                <div>验证意见:</div>
                <div style="height: 80px; position: relative">
                  <template
                    v-if="CheckApprove(scope, 'yzr').ShowMessage"
                  >{{ CheckApprove(scope, "yzr").MessageText }}</template>
                </div>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 18px;
                "
              >
                <div style="width: 55mm; position: relative">
                  验证人：
                  <template v-if="CheckApprove(scope, 'yzr').ShowImg">
                    <img
                      style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'yzr').ApproveImg"
                    />
                  </template>
                </div>
                <div style="width: 50mm">日期：{{ checkHistoryNode(scope, "yzr").endTime }}</div>
              </div>
            </td>
          </tr>
        </table>
        <!-- 底部信息位置 -->

        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import printStyle from "@/utils/print.js";
import { listcorrect, delcorrect } from "@/api/quality/correct.js";
export default {
  name: "correct",
  dicts: ["sys_error_type"],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        operUserId: undefined,
        licenseNum: undefined,
        checkDate: null
      },
      // 日期范围
      dateRange: [],
      infos: [], //勾选的数据
      printList: []
    };
  },
  created() {
    const str = sessionStorage.getItem("queryParams");
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startAuditDate) {
        this.dateRange.push(
          this.queryParams.startAuditDate,
          this.queryParams.endAuditDate
        );
      }
      setTimeout(() => {
        sessionStorage.removeItem("queryParams");
      }, 500);
    }
    this.getList();
  },
  methods: {
    checkItem(e, list) {
      return list.some(_e => _e.errorTypeValue == e);
    },
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        document.title = "不符合报告及纠正措施表 STET/1/9-1";
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
          onPrintDialogClose: () => {
            document.title = process.env.VUE_APP_TITLE;
          }
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          height:12mm;
          padding: 4px;
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.innerCorrectId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.infos = selection;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem("queryParams", JSON.stringify(this.queryParams));
      const configId = row.innerCorrectId || this.ids;
      this.$router.push("./addCorrect?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem("queryParams", JSON.stringify(this.queryParams));
      this.$router.push({
        path: "/correct/process/detail/" + row.piid,
        query: {
          definitionId: null,
          deployId: null,
          taskId: null,
          finished: false,
          id: row.innerCorrectId
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.innerCorrectId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "innerCorrectId",
        "DELETE",
        "year"
      );
      this.$modal
        .confirm('是否确认删除id为"' + configId + '"的数据项？')
        .then(function() {
          return delcorrect(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.$router.push("./addCorrect");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listcorrect(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startAuditDate",
          "endAuditDate"
        )
      ).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleExport() {
      this.download(
        "framework/correct/export",
        {
          ...this.queryParams
        },
        `不符合报告及纠正措施表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
